import pygame
import sys
from settings import Settings
from ship import Ship
from aline import Aline
from bullet import Bullet
from alien_bullets import Alien_bullet
from button import Button

class Ship_againse:
    """管理游戏资源与行为"""
    def __init__(self):
        """初始化游戏并创建游戏资源"""
        pygame.init()
        pygame.display.set_caption("Ship against Alien")
        self.settings = Settings()
        self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
        self.clock = pygame.time.Clock()
        self.ship = Ship(self)
        self.aline = Aline(self)
        self.bullets = pygame.sprite.Group()
        self.alien_bullets = pygame.sprite.Group()
        self.game_active = False
        self.play_button = Button(self, "Play", "Ship Win!", "Alien Win!")

    def run_game(self):
        """开始游戏主循环"""
        while True:
            self._check_events()

            if self.game_active:
                self.ship.update()
                self.aline.aline_update()
                self._update_bullets()
                self._update_alien_bullet()

            self._update_screen()
            self.clock.tick(60)

    def _check_events(self):
        """响应按键和鼠标事件"""
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
                self._check_aline_keydown_event(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)
                self._check_aline_keyup_events(event)
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                self._check_play_button(mouse_pos)

    def _check_play_button(self, mouse_pos):
        if self.play_button.rect.collidepoint(mouse_pos):
            self.game_active = True

    def _check_keydown_events(self, event):
        """响应按下"""
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = True
        if event.key == pygame.K_LEFT:
            self.ship.moving_left = True
        if event.key == pygame.K_UP:
            self.ship.moving_up = True
        if event.key == pygame.K_DOWN:
            self.ship.moving_down = True
        if event.key == pygame.K_SPACE:
            self._fire_bullet()
        if event.key == pygame.K_j:
            self._fire_alien_bullet()

    def _check_aline_keydown_event(self, event):
        if event.key == pygame.K_d:
            self.aline.moving_right = True
        if event.key == pygame.K_a:
            self.aline.moving_left = True
        if event.key == pygame.K_w:
            self.aline.moving_up = True
        if event.key == pygame.K_s:
            self.aline.moving_down = True

    def _check_keyup_events(self, event):
        """响应抬起"""
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = False
        if event.key == pygame.K_LEFT:
            self.ship.moving_left = False
        if event.key == pygame.K_UP:
            self.ship.moving_up = False
        if event.key == pygame.K_DOWN:
            self.ship.moving_down = False

    def _check_aline_keyup_events(self, event):
        if event.key == pygame.K_d:
            self.aline.moving_right = False
        if event.key == pygame.K_a:
            self.aline.moving_left = False
        if event.key == pygame.K_w:
            self.aline.moving_up = False
        if event.key == pygame.K_s:
            self.aline.moving_down = False

    def _fire_bullet(self):
        """创建一颗子弹，并将其加入编组"""
        if len(self.bullets) < self.settings.bullet_allowed:
            new_bullet = Bullet(self)
            self.bullets.add(new_bullet)

    def _fire_alien_bullet(self):
        if len(self.alien_bullets) < self.settings.aline_bullet_allowed:
            new_alien_bullet = Alien_bullet(self)
            self.alien_bullets.add(new_alien_bullet)

    def _update_bullets(self):
        """更新子弹的位置并删除已消失的子弹"""
        # 更新子弹位置
        self.bullets.update()
        rect_1 = self.aline.rect

        # 删除已消失的子弹
        for bullet in self.bullets.copy():
            if bullet.rect.bottom <= 0:
                self.bullets.remove(bullet)
        # 响应飞船子弹与外星人的碰撞
            if rect_1.colliderect(bullet.rect):
                self.settings.alien_life -= 1
            if self.settings.alien_life < 0:
                self.game_active = False

    def _update_alien_bullet(self):
        self.alien_bullets.update()
        rect_2 = self.ship.rect

        for alien_bullet in self.alien_bullets.copy():
            if alien_bullet.rect.y >= 800:
                self.alien_bullets.remove(alien_bullet)
        # 响应外星人子弹与飞船的碰撞
            if rect_2.colliderect(alien_bullet.rect):
                self.settings.ship_life -= 1
            if self.settings.ship_life < 0:
                self.game_active = False

    def _update_screen(self):
        """更新屏幕上的图像，并切换到新的屏幕"""
        self.screen.fill(self.settings.bg_color)
        for alien_bullet in self.alien_bullets.sprites():
            alien_bullet.draw_alien_bullet()
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()
        self.ship.blitme()
        self.aline.draw_aline()

        if not self.game_active:
            self.play_button.draw_button()

        if self.settings.ship_life < 0:
            self.play_button.draw_aline_win()
        if self.settings.alien_life < 0:
            self.play_button.draw_ship_win()

        pygame.display.flip()

if __name__ == '__main__':
    ai = Ship_againse()
    ai.run_game()

